<?php
class SoftDeleteBehavior extends CActiveRecordBehavior 
{
	public $deleteAttribute = 'deleted';
 
    public function beforeDelete($event) 
    {
        if ($this->deleteAttribute !== null) 
        {
            $this->getOwner()->{$this->deleteAttribute} = 1; //// Deleted is a tinyint column. So 0= NOT deleted, 1=DELETED
            $this->getOwner()->save();
 
            // prevent real deletion of record from database
            $event->isValid = false;
        }
    }
    
	public function unDelete() 
	{
        if ($this->deleteAttribute !== null) {
            if ($this->getOwner()->{$this->deleteAttribute} !== null) 
            {
                $this->getOwner()->{$this->deleteAttribute} = 0; // Deleted is a tinyint column. So 0= NOT deleted, 1=DELETED
                $this->getOwner()->save();
            }
        }
    }
    
	public function beforeFind() {
	                if ($this->getEnabled()) {
                        $this->Owner
                        ->getDbCriteria()
                        ->addCondition('deleted != 1');
                }
    }
}
?>